let tds = table.querySelectorAll("td")
let icons = table.querySelectorAll(".icon")
let confirms = table.querySelectorAll(".confirm")

function setLocalstorage(tr, inp, td, span) {
    let trIndex = tr.id
    let tdIndex = inp.getAttribute("index")
    let value = inp.value
    if (!/^-?\d+$|\d+.\d+$/.test(value)) {
        alert("输入有误，请重新输入")
        return false
    }
    let output = getData()
    let product = output[trIndex].product
    let region = output[trIndex].region
    sourceData.map(function (v, i) {
        if (v.product == product && v.region == region) {
            v.sale[tdIndex] = value
        }
    })
    span.innerHTML = value
    localStorage.setItem("sourceData", JSON.stringify(sourceData))
    td.className = ""
}
table.addEventListener("click", function (e) {
    let target = e.target
    let tds = this.querySelectorAll("td")
    let icons = this.querySelectorAll(".icon")
    let cancels = table.querySelectorAll(".cancel")
    let ee = e || window.event
    if (target.nodeName == "TD" && target.querySelector(".edit")) {
        let inp = target.querySelector(".edit input")
        if (target.className == "noactive") {
            return
        }
        for (let i = 0; i < tds.length; i++) {
            const element = tds[i];
            if (element.className == "noactive") {
                continue
            }
            element.className = ""
        }
        target.className = "select"
        inp.focus()
        inp.select()
        ee.stopPropagation ? ee.stopPropagation() : ee.cancelBubble == true
    }
    if (target.nodeName == "SPAN") {
        if (target.className.indexOf("icon") == 0) {
            let td = target.parentNode
            let inp = td.querySelector(".edit input")
            for (let j = 0; j < icons.length; j++) {
                const element = icons[j];
                const td1 = element.parentNode
                td1.className = ""
            }
            td.className = "select"
            ee.stopPropagation ? ee.stopPropagation() : ee.cancelBubble == true
            inp.focus()
            inp.select()
        }
        if (target.className.indexOf("cancel") == 0) {
            let td = target.parentNode.parentNode
            td.className = ""
            ee.stopPropagation ? ee.stopPropagation() : ee.cancelBubble == true
        }
        if (target.className.indexOf("confirm") == 0) {
            let span = target.parentNode.parentNode.children[0]
            let td = target.parentNode.parentNode
            let inp = target.parentNode.children[0]
            let tr = target.parentNode.parentNode.parentNode
            let flag = setLocalstorage(tr, inp, td, span)
            if (!flag) {
                inp.focus()
                inp.select()
            }
            ee.stopPropagation ? ee.stopPropagation() : ee.cancelBubble == true
        }
    }
})
table.addEventListener("keydown", function (e) {
    let ee = e || window.event
    let target = ee.target
    if (target.nodeName == "INPUT") {
        console.log(e.keyCode);
        // 27 13
        if (e.keyCode == 27) {
            let td = target.parentNode.parentNode
            td.className = ""
        }
        if (e.keyCode == 13) {
            let inp = target
            let td = target.parentNode.parentNode
            let tr = td.parentNode
            let span = td.children[0]
            setLocalstorage(tr, inp, td, span)
        }
        ee.stopPropagation ? ee.stopPropagation() : ee.cancelBubble == true
    }
})
document.addEventListener("click", function (e) {
    let event = e || window.event
    let target = event.target
    let tds = table.querySelectorAll("td")
    for (let i = 0; i < tds.length; i++) {
        const td = tds[i];
        td.className = ""
    }
    event.stopPropagation ? event.stopPropagation() : event.cancelBubble == true
})